package com.js.comment.service.Impl;

import com.js.comment.entity.Comment;
import com.js.comment.mapper.CommentMapper;
import com.js.comment.service.CommentService;
import com.js.comment.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;


import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;


import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private MongoTemplate mongoTemplate;

    LocalDateTime dateTime = LocalDateTime.now();

    /**
     * 查询所有评论
     * @return
     */
    @Override
    public List<Comment> selectAllArticleComment() {
        return commentMapper.findAll();
    }

    /**
     * 添加评论
     * @param comment
     */
    @Override
    public void saveComment(Comment comment) {
        comment.setArticleid("100001");
        comment.setCreatedatetime(dateTime);
        commentMapper.save(comment);
    }

    /**
     * 删除评论
     * @param id
     */
    @Override
    public void deleteCommentById(String id) {
        commentMapper.deleteById(id);
    }


    @Override
    public List<Comment> selectById(String id) {
        List<Comment> list = (List<Comment>) commentMapper.findById(id).get();
        return list;
    }


    /**
     * 点赞 +1
     * @param id
     */
    @Override
    public void updateCommentLikenumThumbup(String id) {
        //查询对象
        Query query=Query.query(Criteria.where("_id").is(id));
        //更新条件
        Update update = new Update();
        update.inc("likenum");

        //执行更新
        mongoTemplate.updateFirst(query,update,"comment");
    }


    @Override
    public List<Comment> selectAllCommentId(String parentid) {
        //查询对象
        Query query=Query.query(Criteria.where("parentid").is(parentid));
        List<Comment> list = mongoTemplate.find(query,Comment.class);
        return list;
    }
}
